home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / irafimtl.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  9KB  |  268 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    irafimtl.c (IRAF Imtool)
  6.  * Purpose:    Respond to input from IRAF meant for Imtool
  7.  * Subroutine:    imtool_response()    returns: void
  8.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  9.  *        You may do anything you like with this file except remove
  10.  *        this copyright.  The Smithsonian Astrophysical Observatory
  11.  *        makes no representations about the suitability of this
  12.  *        software for any purpose.  It is provided "as is" without
  13.  *        express or implied warranty.
  14.  * Modified:    {0} based on code from Doug Tody's IRAF Imtool (NOAO)
  15.  *        {1} Michael VanHilst    adapted            9 July 1989
  16.  *        {2} MVH Hacked for Ultrix fifo problems        6 Oct 1989
  17.  *        {3} Jay Travisano (STScI)  VMS,DEBUG_IMTOOL     10 Nov 1989
  18.  *        {n} <who> -- <does what> -- <when>
  19.  * "For the moment we take an IIS model 70 command/data stream as input; this
  20.  * is used to load images into the image display.  This is a kludge interface
  21.  * for the prototype, convenient since the high level software is written for
  22.  * the IIS." - explanation by Doug Tody
  23.  */
  24.  
  25. #ifdef IMTOOL
  26.  
  27. #include <stdio.h>        /* stderr, FILE, NULL, etc. */
  28. #include <X11/Xlib.h>        /* get X types and constants */
  29. #include <X11/Xutil.h>        /* X window manager stuff */
  30. #include "hfiles/constant.h"    /* define codes */
  31. #include "hfiles/struct.h"    /* declare structure types */
  32. #include "hfiles/extern.h"    /* extern main ximage parameter structures */
  33. #include "hfiles/imtool.h"
  34.  
  35.  
  36. static int frame_number = 1;
  37. static int packet_y1, packet_y2;
  38. static int imtool_cols=512;
  39. static int imtool_rows=512;
  40. static int packet_cols=512;
  41. static int wcs_piped=0;        /* l: set if wcs info sent through pipe */
  42. static char wcsbuf[SZ_WCSBUF];
  43.  
  44. /*
  45.  * Subroutine:    imtool_response
  46.  * Purpose:    Read imtool messages from iraf (return 1 if pipe trouble)
  47.  */
  48. void imtool_response ( port, imhead, ndatabytes )
  49.      struct connectRec *port;    /* i: struct of port for IRAF input */
  50.      struct imtoolRec *imhead;    /* i: packet header, already read in */
  51.      int ndatabytes;        /* i: number of bytes in expected message */
  52. {
  53.   char ibuf[SZ_FIFOBUF];
  54.   int bytes, n;
  55.   int temp;
  56.  
  57.   int imtool_input(), write_connection(), update_wcs(), read_connection();
  58.   void imtool_newimage(), map_dispbox(), disp_dispbox();
  59.   void set_curpos_to_iraf_trigger(), set_cursor_from_iraf(), imtool_output();
  60.   void set_imtool_scale(), set_cursor_to_iraf(), disp_subpiece();
  61.   void map_panbox(), disp_panbox(), send_curpos_to_iraf(), flush_connection();
  62.   static int get_frame_no();
  63.  
  64.   switch( imhead->subunit & 077 ) {
  65.   case FEEDBACK:
  66.     /* The IIS feedback unit is used to clear the screen */
  67.     bzero(buffer.filebuf, buffer.filebuf_sz);
  68.     if( buffer.filebuf != (char *)buffer.shortbuf )
  69.       bzero((char *)buffer.shortbuf, buffer.shortbuf_sz * sizeof(short));
  70.     map_dispbox();
  71.     disp_dispbox();
  72.     /* clear the panbox */
  73.     bzero((char *)buffer.panbuf, buffer.panbuf_sz * sizeof(short));
  74.     map_panbox();
  75.     disp_panbox();
  76.     /* this must be a new start, so reset everything just in case we haven't */
  77.     imtool_newimage(0, imhead);
  78.     break;
  79.   case WCS:
  80.     /* Read or write the WCS for a frame.  The frame number to
  81.      * which the WCS applies is passed in Z and the frame buffer
  82.      * configuration in T.  The client changes the frame buffer
  83.      * configuration in a WCS set.  The WCS text follows the header
  84.      * as byte packed ASCII data.
  85.      */
  86.     if( imhead->tid & IIS_READ ) {
  87.       if( control.IRAF_out.open ) {
  88. #ifdef FRAMERR
  89. /* include this code if requested frame must match our wcs  */
  90.     /* Return the WCS for the referenced frame. */
  91.     int frame;
  92.     frame = get_frame_no(imhead->z);
  93.     if( frame != fb_frameno ) {
  94.       char emsg[SZ_WCSBUF];
  95.       (void)strcpy(emsg, "[NOSUCHFRAME]\n");
  96.       (void)write_connection(&control.IRAF_out, emsg, SZ_WCSBUF);
  97.     } else
  98. #endif
  99.       (void)write_connection(&control.IRAF_out, wcsbuf, SZ_WCSBUF);
  100.       } else
  101.     (void)fprintf(stderr, "Warning: output pipe not open\n");
  102.     } else {
  103.       int new_wcs;
  104.       char buf[SZ_WCSBUF];
  105.  
  106.       /* Read in and set up the WCS. */
  107.       if( (bytes = read_connection(port, buf, ndatabytes))
  108.      == ndatabytes ) {
  109.     if( strncmp(wcsbuf, buf, SZ_WCSBUF) != 0 )
  110.       new_wcs = 1;
  111.     else
  112.       new_wcs = 0;
  113.     strncpy(wcsbuf, buf, SZ_WCSBUF);
  114.     wcs_piped = update_wcs(&img, &coord, frame_number, wcsbuf);
  115.     if( new_wcs )
  116.       imtool_newimage(1, imhead);
  117.       }
  118.       ndatabytes -= bytes;
  119.     }
  120.     break;
  121.   case IMCURSOR:
  122.     /* Read or write the logical image cursor.  This is an extension
  123.      * added to provide a high level cursor read facility; this is
  124.      * not the same as a low level access to the IIS cursor subunit.
  125.      * Cursor reads may be either nonblocking (immediate) or blocking,
  126.      * using the keyboard or mouse to terminate the read, and
  127.      * coordinates may be returned in either image (world) or frame
  128.      * buffer pixel coordinates.
  129.      */
  130.     frame_number = get_frame_no(imhead->z);
  131.     if( imhead->tid & IIS_READ ) {
  132.       /* Read the logical image cursor.  In the case of a blocking
  133.        * read all we do is initiate a cursor read; completion occurs
  134.        * when the user hits a key or button.
  135.        */
  136.       if( imhead->tid & IMC_SAMPLE ) {
  137.     /* Sample the cursor position. */
  138.     /* Return the cursor value on the output datastream encoded
  139.      * in a fixed size ascii buffer.
  140.      */
  141.     send_curpos_to_iraf(cursor.file.X, cursor.file.Y, frame_number,
  142.                 imhead->z, 0, "");
  143.       } else {
  144.     /* Initiate a user triggered cursor read. */
  145.     set_curpos_to_iraf_trigger(frame_number, imhead->z);
  146.       }
  147.     } else
  148.       set_cursor_from_iraf((double)(imhead->x & 077777),
  149.                (double)(imhead->y & 077777));
  150.     break;
  151.   case MEMORY:
  152.     frame_number = get_frame_no(imhead->z);
  153.     if( imhead->tid & IIS_READ ) {
  154.       imtool_output(imhead, &control.IRAF_out, buffer.shortbuf,
  155.             img.filecols, img.filerows);
  156.     } else {
  157.       temp = imtool_input(imhead, &control.IRAF_in, ibuf,
  158.               packet_cols, imtool_cols, &packet_y1, &packet_y2,
  159.               (short *)buffer.filebuf, buffer.shortbuf_sz * 2);
  160.       if( img.imtool_200 == 0 ) {
  161.     /* if we were caught unawares! */
  162.     img.imtool_200 = 1;
  163.     set_imtool_scale();
  164.       }
  165.       if( temp >= 0 )
  166.     ndatabytes = 0;
  167.       /* if not doing full reset, map and display just this piece */
  168.       disp_subpiece(0, packet_y1, imtool_cols - 1, packet_y2 - 1);
  169.     }
  170.     if( ndatabytes == 0 )
  171.       return;
  172.   default:
  173.     /* Ignore unsupported command input. */
  174.     break;
  175.   }
  176.   /* Discard any extra data */
  177.   if( !(imhead->tid & IIS_READ) ) {
  178.     for( bytes = ndatabytes; bytes > 0; bytes -= n ) {
  179.       n = (bytes < SZ_FIFOBUF) ? bytes : SZ_FIFOBUF;
  180.       if( (n = read_connection(&control.IRAF_in, ibuf, n)) <= 0 )
  181.     break;
  182.     }
  183.   }
  184. }
  185.  
  186. /*
  187.  * Procedure:    imtool_newimage
  188.  * Purpose:    Reset buffer sizes, coordinate systems and parameters as
  189.  *        needed.
  190.  */
  191. void imtool_newimage ( is_wcs, imhead )
  192.      int is_wcs;
  193.      struct imtoolRec *imhead;
  194. {
  195.   int fbconfig;
  196.   int cols, rows;
  197.   int get_fbconfig(), update_wcs();
  198.   void set_disptran(), combine_transform(), set_edges(), set_dispoff();
  199.   void adjust_cursor_coords(), set_magnifier(), disp_subpiece(), set_tdisp();
  200.   void set_imtool_scale(), imtool_reinit(), new_pancursor();
  201.   static int get_frame_no();
  202.  
  203.   /* set size and frame */
  204.   if( is_wcs )
  205.     fbconfig = (imhead->t & 077) + 1;
  206.   else
  207.     fbconfig = (imhead->tid & IMT_FBCONFIG) + 1;
  208.   frame_number = get_frame_no(imhead->z);
  209.   /* config values assume a zero based array, table starts at 1 */
  210.   if( img.fbconfig != fbconfig ) {
  211.     img.fbconfig = fbconfig;
  212.     /* reset defaults if failed on config set and no known size */
  213.     if( (get_fbconfig(fbconfig, &cols, &rows) == 0) && (cols <=0) ) {
  214.       (void)get_fbconfig(0, &cols, &rows);
  215.     }
  216.     packet_cols = cols;
  217.     imtool_cols = cols;
  218.     imtool_rows = rows;
  219.   }
  220.   /* fix up img coordsys parameters and make sure enough space is alloc'd */
  221.   imtool_reinit(imtool_cols, imtool_rows);
  222.   /* reinit sets imgtofile to a default, restore true wcs if we know it */
  223.   if( wcs_piped )
  224.     (void)update_wcs(&img, &coord, frame_number, wcsbuf);
  225.   /* update the coords right away (correct or default) */
  226.   /* set display params to center the new image with zoom 1 */
  227.   coord.tid.cenX = coord.img.cenX;
  228.   coord.tid.cenY = coord.img.cenY;
  229.   coord.tid.zoom = 1.0;
  230.   set_tdisp(&coord);
  231.   /* set disp to img and disp to file transforms */
  232.   set_disptran(&coord);
  233.   /* set transform to get from disp to buf coords */
  234.   combine_transform(&coord.disptobuf, &coord.disptoimg, &coord.imgtobuf);
  235.   set_edges(&coord.disptobuf, &coord.buf, &coord.disp, &coord.bd);
  236.   /* redefine the edges in the display window if at edge of buffer */
  237.   if( (coord.bd.block < 0) || (coord.bd.clip != 0) )
  238.     set_dispoff(&coord.disptobuf, &coord.disp, &coord.bd);
  239.   /* update the panbox cursor (again) since we reset the display center */
  240.   new_pancursor(0);
  241.   /* update cursor and magnifier info to correspond to current display */
  242.   adjust_cursor_coords(&cursor, &coord);
  243.   set_magnifier();
  244.   /* set scale and colors for the standard imtool range and options */
  245.   img.imtool_200 = 1;
  246.   set_imtool_scale();
  247. }
  248.  
  249. /*
  250.  * Subroutine:    get_frame_no
  251.  * Purpose:    Figure out the integer frame number for its mask
  252.  */
  253. static int get_frame_no ( mask )
  254.      int mask;
  255. {
  256.   int bit, bitmask;
  257.  
  258.   mask = mask & 07777;
  259.   bit = 0;
  260.   bitmask = 1;
  261.   while( bitmask <= mask ) {
  262.     bitmask = bitmask << 1;
  263.     ++bit;
  264.   }
  265.   return( bit );
  266. }
  267. #endif
  268.